home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2 Examples.sit / Raven 1.2 Examples / BoxPaint / Source / Pencil.cpp < prev    next >
Text File  |  1997-03-09  |  11KB  |  361 lines

  1. /*
  2.  *  File:       Pencil.cpp
  3.  *  Summary:    A 3D mouse cursor in the shape of a pencil.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *  Copyright ゥ 1997 Jesse Jones. 
  7.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  8.  *
  9.  *  Change History (most recent first):
  10.  *
  11.  *         <->      3/09/97    JDJ        Created.
  12.  */
  13.  
  14. #include "Pencil.h"
  15.  
  16. #include <Z3DCone.h>
  17. #include <Z3DCylinder.h>
  18. #include <Z3DIlluminationShaders.h>
  19. #include <Z3DLine.h>
  20. #include <Z3DRenderingLoops.h>
  21. #include <Z3DTransforms.h>
  22. #include <Z3DView.h>
  23.  
  24.  
  25. // ===================================================================================
  26. //    class CPencil
  27. // ===================================================================================
  28.  
  29. //---------------------------------------------------------------
  30. //
  31. // CPencil::~CPencil
  32. //
  33. //---------------------------------------------------------------
  34. CPencil::~CPencil()
  35. {
  36. }
  37.  
  38.  
  39. //---------------------------------------------------------------
  40. //
  41. // CPencil::CPencil
  42. //
  43. //---------------------------------------------------------------
  44. CPencil::CPencil()
  45. {
  46.     mShowOrientation = false;                    // don't show orientation
  47.  
  48.     mCursorPlacement.location = kZero3DPt;
  49.     mCursorPlacement.toward   = T3DVector(0.0, 0.0, 1.0);
  50.     mCursorPlacement.up       = T3DVector(0.0, 1.0, 0.0);
  51.  
  52.     mTransform = kIdentity3DMatrix;                // no transform
  53.     
  54.     mScale.x = 1.0;                                // no scale
  55.     mScale.y = 1.0;
  56.     mScale.z = 1.0;    
  57.     
  58.     // Create the pencil model.    
  59.     this->CreatePencil();
  60.     
  61.     // Don't want to be able to pick the cursor.    
  62.     mPencilModel.RemoveState(kQ3DisplayGroupStateMaskIsPicked);
  63.  
  64.     // Since the pencil doesn't have any attributes QD 3D doesn't
  65.     // need to push and pop the graphics state when rendering the
  66.     // pencil.
  67.     mPencilModel.AddState(kQ3DisplayGroupStateMaskIsInline);
  68.     
  69.     // Create the orientation indicator.
  70.     this->CreateOrientationModel();
  71.  
  72.     // Like the pencil model the orientation model shouldn't be
  73.     // picked and we can render it inline.
  74.     mOrientationModel.RemoveState(kQ3DisplayGroupStateMaskIsPicked);
  75.     mOrientationModel.AddState(kQ3DisplayGroupStateMaskIsInline);
  76.         
  77.     // Make the pencil transparent
  78.     T3DColorRGB    color(0.35, 0.35, 0.35);
  79.     mCursorAttributeSet.AddTransparencyColor(color);
  80.     
  81.     // Finally, we don't want the cursor to be included in bounding operations
  82.     mPencilModel.AddState(kQ3DisplayGroupStateMaskUseBoundingBox | kQ3DisplayGroupStateMaskUseBoundingSphere);
  83.     mOrientationModel.AddState(kQ3DisplayGroupStateMaskUseBoundingBox | kQ3DisplayGroupStateMaskUseBoundingSphere);
  84.     
  85.     this->Hide();
  86. }
  87.  
  88.  
  89. //---------------------------------------------------------------
  90. //
  91. // CPencil::Show
  92. //
  93. //---------------------------------------------------------------
  94. void CPencil::Show()
  95. {    
  96.     mPencilModel.AddState(kQ3DisplayGroupStateMaskIsDrawn);
  97.     mOrientationModel.AddState(kQ3DisplayGroupStateMaskIsDrawn);
  98. }
  99.  
  100.  
  101. //---------------------------------------------------------------
  102. //
  103. // CPencil::Hide
  104. //
  105. //---------------------------------------------------------------
  106. void CPencil::Hide()
  107. {    
  108.     mPencilModel.RemoveState(kQ3DisplayGroupStateMaskIsDrawn);
  109.     mOrientationModel.RemoveState(kQ3DisplayGroupStateMaskIsDrawn);
  110. }
  111.  
  112.  
  113. //---------------------------------------------------------------
  114. //
  115. // CPencil::MoveTo
  116. //
  117. //---------------------------------------------------------------
  118. void CPencil::MoveTo(const S3DCursorLocation& loc)
  119. {
  120.     T3DMatrix matrix = kIdentity3DMatrix;
  121.  
  122.     // Get the last vector we need
  123.     T3DVector xAxis = CrossProduct(loc.up, loc.toward);
  124.         
  125.     // This is a neat graphics trick stuff the normals into the 
  126.     // matrix and it will automatically place your cursor with all 
  127.     // the correct orientation.
  128.     matrix.value[0][0] = xAxis.x;
  129.     matrix.value[0][1] = xAxis.y;
  130.     matrix.value[0][2] = xAxis.z;
  131.     
  132.     matrix.value[1][0] = loc.up.x;
  133.     matrix.value[1][1] = loc.up.y;
  134.     matrix.value[1][2] = loc.up.z;
  135.  
  136.     matrix.value[2][0] = loc.toward.x;
  137.     matrix.value[2][1] = loc.toward.y;
  138.     matrix.value[2][2] = loc.toward.z;
  139.     
  140.     //    Stuff in the translation too
  141.     matrix.value[3][0] = loc.location.x;
  142.     matrix.value[3][1] = loc.location.y;
  143.     matrix.value[3][2] = loc.location.z;
  144.  
  145.     mTransform = matrix;
  146. }
  147.  
  148.  
  149. //---------------------------------------------------------------
  150. //
  151. // CPencil::Submit
  152. //
  153. //---------------------------------------------------------------
  154. void CPencil::Submit(const T3DView& view)
  155. {
  156.     mCursorAttributeSet.Submit(view);
  157.     
  158.     T3DMatrixTransform::Submit(mTransform, view);
  159.     T3DScaleTransform::Submit(mScale, view);
  160.     
  161.     mOrientationModel.Submit(view);
  162.     mPencilModel.Submit(view);
  163. }
  164.  
  165.  
  166. //---------------------------------------------------------------
  167. //
  168. // CPencil::Scale
  169. //
  170. //---------------------------------------------------------------
  171. void CPencil::Scale(const T3DDisplayGroup& model, const T3DView& view, float scale)
  172. {
  173.     // For the purpose of this, we want to enable bounding
  174.     mPencilModel.AddState(kQ3DisplayGroupStateMaskUseBoundingBox);
  175.     mOrientationModel.AddState(kQ3DisplayGroupStateMaskUseBoundingBox);
  176.     
  177.     // Get the two bounding boxes
  178.     T3DRect cursorBox;
  179.     T3DBoundingBoxLoop loop1(view, &cursorBox, kQ3ComputeBoundsExact);
  180.     do {
  181.         mPencilModel.Submit(view);
  182.     } while (loop1.Rendering());
  183.  
  184.     T3DRect groupBox;
  185.     T3DBoundingBoxLoop loop2(view, &groupBox, kQ3ComputeBoundsExact);
  186.     do {
  187.         model.Submit(view);
  188.     } while (loop2.Rendering());
  189.     
  190.     // Make sure there's stuff in it
  191.     if (!cursorBox.IsEmpty() && !groupBox.IsEmpty()) {
  192.         float theRatio = Distance(kZero3DPt, groupBox.size)/Distance(kZero3DPt, cursorBox.size);
  193.  
  194.         // We scale it to match the model and then scale it based on the parameter
  195.         theRatio = fabs(theRatio)*scale;
  196.     
  197.         mScale = T3DVector(theRatio, theRatio, theRatio);
  198.     }
  199.  
  200.     // Don't forget to disable it when we are done
  201.     mPencilModel.RemoveState(kQ3DisplayGroupStateMaskUseBoundingBox);
  202.     mOrientationModel.RemoveState(kQ3DisplayGroupStateMaskUseBoundingBox);
  203. }
  204.  
  205. #pragma mark ハ
  206.  
  207. //---------------------------------------------------------------
  208. //
  209. // CPencil::CreatePencil
  210. //
  211. //---------------------------------------------------------------
  212. void CPencil::CreatePencil()
  213. {
  214.     mPencilModel.AddObject(T3DLambertShader());
  215.     
  216.     //                            
  217.     //    HOW TO MAKE A PENCIL    
  218.     //                            
  219.     mPencilModel.AddObject(T3DInterpolationStyle(kQ3InterpolationStyleVertex));
  220.     mPencilModel.AddObject(T3DSubdivisionStyle(kQ3SubdivisionMethodConstant, 6, 1));
  221.     
  222.     //    THE GRAPHITE    
  223.     T3DAttributeSet graphiteAttributes;
  224.     T3DColorRGB    color(0.25, 0.25, 0.25);
  225.     graphiteAttributes.AddDiffuseColor(color);
  226.     
  227.     TQ3ConeData    coneData;
  228.     coneData.origin               = T3DPoint(0, 0, -1.0);
  229.     coneData.orientation          = T3DVector(0, 0, 1.0);
  230.     coneData.majorRadius          = T3DVector(0.26, 0.0, 0);
  231.     coneData.minorRadius          = T3DVector(0.0, 0.26, 0);
  232.     coneData.uMin                 = 0.0; 
  233.     coneData.uMax                 = 1.0;
  234.     coneData.vMin                 = 0.0; 
  235.     coneData.vMax                 = 1.0;
  236.     coneData.caps                 = kQ3EndCapNone;
  237.     coneData.interiorAttributeSet = nil;
  238.     coneData.bottomAttributeSet   = nil;
  239.     coneData.faceAttributeSet     = nil;
  240.     coneData.coneAttributeSet     = graphiteAttributes;
  241.     
  242.     T3DCone graphite(coneData);        
  243.     mPencilModel.AddObject(graphite);
  244.     
  245.     //    THE WOOD    
  246.     T3DAttributeSet woodAttributes;
  247.     color = T3DColorRGB(0.75, 0.75, 0.25);
  248.     woodAttributes.AddDiffuseColor(color);
  249.     
  250.     coneData.origin               = T3DPoint(0, 0, -2.0);
  251.     coneData.orientation          = T3DVector(0, 0, 2.0);
  252.     coneData.majorRadius          = T3DVector(0.5, 0.0, 0);
  253.     coneData.minorRadius          = T3DVector(0.0, 0.5, 0);
  254.     coneData.uMin                 = 0.0; 
  255.     coneData.uMax                 = 1.0;
  256.     coneData.vMin                 = 0.0; 
  257.     coneData.vMax                 = 1.0;
  258.     coneData.caps                 = kQ3EndCapNone;
  259.     coneData.interiorAttributeSet = nil;
  260.     coneData.bottomAttributeSet   = nil;
  261.     coneData.faceAttributeSet     = nil;
  262.     coneData.coneAttributeSet     = woodAttributes;
  263.     
  264.     T3DCone wood(coneData);        
  265.     mPencilModel.AddObject(wood);
  266.         
  267.     //    THE ERASER        
  268.     mPencilModel.AddObject(T3DSubdivisionStyle(kQ3SubdivisionMethodConstant, 12, 1));
  269.     
  270.     T3DAttributeSet eraserAttributes;
  271.     color = T3DColorRGB(1.0, 0.5, 0.5);
  272.     eraserAttributes.AddDiffuseColor(color);
  273.     
  274.     TQ3CylinderData    cylinderData;
  275.     cylinderData.origin               = T3DPoint(0, 0, -7.0);
  276.     cylinderData.orientation          = T3DVector(0, 0, 1.0);
  277.     cylinderData.majorRadius          = T3DVector(0.48, 0.0, 0);
  278.     cylinderData.minorRadius          = T3DVector(0.0, 0.48, 0);
  279.     cylinderData.uMin                 = 0.0; 
  280.     cylinderData.uMax                 = 1.0;
  281.     cylinderData.vMin                 = 0.0; 
  282.     cylinderData.vMax                 = 1.0;
  283.     cylinderData.caps                 = kQ3EndCapMaskBottom;
  284.     cylinderData.interiorAttributeSet = nil;
  285.     cylinderData.topAttributeSet      = nil;
  286.     cylinderData.bottomAttributeSet   = nil;
  287.     cylinderData.faceAttributeSet     = nil;
  288.     cylinderData.cylinderAttributeSet = eraserAttributes;
  289.     
  290.     T3DCylinder eraser(cylinderData);
  291.     mPencilModel.AddObject(eraser);
  292.         
  293.     //    THE YELLOW PAINT        
  294.     mPencilModel.AddObject(T3DSubdivisionStyle(kQ3SubdivisionMethodConstant, 6, 1));    
  295.     mPencilModel.AddObject(T3DInterpolationStyle(kQ3InterpolationStyleNone));
  296.     
  297.     T3DAttributeSet paintAttributes;
  298.     color = T3DColorRGB(1.0, 1.0, 0.0);
  299.     paintAttributes.AddDiffuseColor(color);
  300.     
  301.     cylinderData.origin               = T3DPoint(0, 0, -6.0);
  302.     cylinderData.orientation          = T3DVector(0, 0, 4.0);
  303.     cylinderData.majorRadius          = T3DVector(0.5, 0.0, 0);
  304.     cylinderData.minorRadius          = T3DVector(0.0, 0.5, 0);
  305.     cylinderData.uMin                 = 0.0; 
  306.     cylinderData.uMax                 = 1.0;
  307.     cylinderData.vMin                 = 0.0; 
  308.     cylinderData.vMax                 = 1.0;
  309.     cylinderData.caps                 = kQ3EndCapNone;
  310.     cylinderData.interiorAttributeSet = nil;
  311.     cylinderData.topAttributeSet      = nil;
  312.     cylinderData.bottomAttributeSet   = nil;
  313.     cylinderData.faceAttributeSet     = nil;
  314.     cylinderData.cylinderAttributeSet = paintAttributes;
  315.                             
  316.     T3DCylinder paint(cylinderData);
  317.     mPencilModel.AddObject(paint);
  318. }
  319.  
  320.  
  321. //---------------------------------------------------------------
  322. //
  323. // CPencil::CreateOrientationModel
  324. //
  325. //---------------------------------------------------------------
  326. void CPencil::CreateOrientationModel()
  327. {
  328.     mOrientationModel.AddObject(T3DLambertShader());
  329.     
  330.     //                                    
  331.     //    HOW TO CREATE 3 SIMPLE LINES    
  332.     //                                    
  333.     
  334.     //    The x,y, and z axis are colored r,g and b, respectively    
  335.     T3DAttributeSet attributes1;
  336.     T3DColorRGB    color(1.0, 0.75, 0.75);
  337.     attributes1.AddDiffuseColor(color);
  338.  
  339.     T3DLine line1(T3DPoint(0.0, 0.0, -0.01), T3DPoint(2.0, 0.0, -0.01), attributes1);
  340.     mOrientationModel.AddObject(line1);
  341.     
  342.     
  343.     T3DAttributeSet attributes2;
  344.     color = T3DColorRGB(0.75, 1.0, 0.75);
  345.     attributes2.AddDiffuseColor(color);
  346.  
  347.     T3DLine line2(T3DPoint(0.0, 0.0, -0.01), T3DPoint(0.0, 2.0, -0.01), attributes2);
  348.     mOrientationModel.AddObject(line2);
  349.     
  350.     
  351.     T3DAttributeSet attributes3;
  352.     color = T3DColorRGB(0.75, 0.75, 1.0);
  353.     attributes3.AddDiffuseColor(color);
  354.  
  355.     T3DLine line3(T3DPoint(0.0, 0.0, -0.01), T3DPoint(0.0, 0.0, 2.0), attributes3);
  356.     mOrientationModel.AddObject(line3);
  357. }
  358.  
  359.  
  360.  
  361.